235A - LCM Challenge - CodeForces Solution


number theory *1600

Please click on ads to support us..

Python Code:

def GCD(a,b):
    if(b==0):
        return a
    return GCD(b,a%b)
n=int(input())
if n==1:
    print(1)
elif(n==2):
    print(2)
else:
    if n&1:
        print(n*(n-1)*(n-2))
    else:
        if(GCD(n,n-3)==1):
            print(n*(n-3)*(n-1))
        else:
            print((n-1)*(n-2)*(n-3))

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define int long long
using ll = long long;
using vb = vector<bool>;
using vvb = vector<vb>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vc = vector<char>;
using vvc = vector<vc>;
using vs = vector<string>;
#define mp make_pair
#define fi first
#define se second
#define fr(n) for(int i=0;i<n;i++)
#define fri(n) for(int i=1;i<=n;i++)
#define pb push_back
#define max(a,b) ((a)>(b)? (a):(b))
#define min(a,b) ((a)<(b)? (a):(b))
#define all(v) v.begin(),v.end()
#define rall(x) (x).rbegin(), (x).rend()


#define N 2e5 + 5
const int MAXN = 500005;
const int MOD = 1e9+7;



// int dfs(int node,int prev,vi &dist)
// {
//     visited[node]=true;
//     int next=0;
//     for(auto i: tree[node])
//     {
//         if(!visited[i])
//             next+=dfs(i,prev+1,dist);
//     }
//     dist.push_back(prev-next);
//     return next+1;
// }



int lcm(int a, int b){
    return (a*b)/__gcd(a,b); 
}


 void solve(){
    int n;
    cin>>n;
    if(n==1){
        cout<<1<<endl;
        return;
    }
    if(n==2){
        cout<<2<<endl;
        return;
    }
    if(n==3){
        cout<<6<<endl;
        return;
    }

    if(n%2!=0){
        cout<<n*(n-1)*(n-2);
        return;
    }
    else{
        if(n%3==0){
            cout<<(n-1)*(n-2)*(n-3);
        }
        else{
            cout<<(n)*(n-1)*(n-3);
        }

    }
    return;


 }

signed main(){
    IOS
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif
    int tests;
    // cin>>tests;
    tests =1;
    while(tests--){
        solve();
    }
}


Comments

Submit
0 Comments
More Questions

74. Search a 2D Matrix
71. Simplify Path
62. Unique Paths
50. Pow(x, n)
43. Multiply Strings
34. Find First and Last Position of Element in Sorted Array
33. Search in Rotated Sorted Array
17. Letter Combinations of a Phone Number
5. Longest Palindromic Substring
3. Longest Substring Without Repeating Characters
1312. Minimum Insertion Steps to Make a String Palindrome
1092. Shortest Common Supersequence
1044. Longest Duplicate Substring
1032. Stream of Characters
987. Vertical Order Traversal of a Binary Tree
952. Largest Component Size by Common Factor
212. Word Search II
174. Dungeon Game
127. Word Ladder
123. Best Time to Buy and Sell Stock III
85. Maximal Rectangle
84. Largest Rectangle in Histogram
60. Permutation Sequence
42. Trapping Rain Water
32. Longest Valid Parentheses
Cutting a material
Bubble Sort
Number of triangles
AND path in a binary tree
Factorial equations